package ch.ethz.fcl.metrobuzz.data;

import ch.ethz.fcl.metrobuzz.MBContainer;
import ch.ethz.fcl.metrobuzz.MBProperty;
import ch.ethz.fcl.metrobuzz.algorithm.network.all_simple_paths.EdgeODPossibility;
import ch.ethz.fcl.metrobuzz.algorithm.network.shortest_path.public_transport.AllToAllDijkstraPublic;
import ch.ethz.fcl.metrobuzz.data.accessibility.activity.MBActivity;
import ch.ethz.fcl.metrobuzz.data.accessibility.cost.MBCost;
import ch.ethz.fcl.metrobuzz.data.loader.plan.PlanFileLoader;
import ch.ethz.fcl.metrobuzz.data.loader.raw.RawEZLinkLoader;
import ch.ethz.fcl.metrobuzz.data.ltt.TrajectoryGraph;
import ch.ethz.fcl.metrobuzz.data.roadnetwork.RoadNetwork;
import ch.ethz.fcl.metrobuzz.data.scene.MBScene;
import ch.ethz.fcl.metrobuzz.data.singapore.Singapore;
import ch.ethz.fcl.metrobuzz.data.texture.MBTexture;
import ch.ethz.fcl.metrobuzz.data.transit.PublicTransit;
import ch.ethz.fcl.metrobuzz.data.transit.TransitLine;
import ch.ethz.fcl.metrobuzz.data.transit.TransitRoute;

public class MBData {
	private MBContainer container;

	private MBScene scene = null;
	private RoadNetwork rn = null;
	private PlanFileLoader agentLoader = null;

	private PublicTransit transitNetwork;

	private MBTexture textures = null;
	private MBCost cost = null;

	private Singapore sgInfo;

	private AllToAllDijkstraPublic allToAllDijkstra;

	private MBActivity activity = null;

	private TrajectoryGraph trjGraph;

	private EdgeODPossibility edgeODPoss;

	public static boolean TIME_CHANGED = false;
	public static final int TIME_RESOLUTION = 15; // time resolution, 15 minutes
	public static final int ONE_DAY_TIME = 24 * 60 / TIME_RESOLUTION; // one day
	// trip starts at 6
	public static final int TRIP_STRAT_TIME = 6 * 60 / TIME_RESOLUTION;

	public static int startTime = 8 * 60 / TIME_RESOLUTION; // start at 8 am
	public static int period = 1; // start time period of 15 minutes

	public MBData(MBContainer container) {
		this.container = container;
		container.setData(this);

		scene = new MBScene(this);

		rn = new RoadNetwork(this);

		transitNetwork = new PublicTransit(this);

		agentLoader = new PlanFileLoader(this, MBProperty.PLAN_FILE);

		sgInfo = new Singapore(this);

		trjGraph = new TrajectoryGraph(this);

		edgeODPoss = new EdgeODPossibility(this);

		allToAllDijkstra = new AllToAllDijkstraPublic(this);

		if (MBProperty.ACCESSIBILITY) {
			cost = new MBCost(this);

			if (MBProperty.ACTIVITY)
				activity = new MBActivity(this);
		}
	}

	public void init() {
		scene.cleanup();

		rn.init();
		transitNetwork.init();
		sgInfo.init();

		if (MBProperty.OD)
			edgeODPoss.generateEdgeODPoss();

		if (MBProperty.LOAD_PLAN)
			agentLoader.loadAgents();

		new RawEZLinkLoader().load(this, MBProperty.RAW_FILE);
		
		if (MBProperty.ACTIVITY)
			activity.init();

		if (MBProperty.ACCESSIBILITY) {
			container.getRender().getAccessibilityRender().update();

			for (TransitLine line : transitNetwork.getLines())
				for (TransitRoute route : line.getRoutes())
					route.calStopTime();
		}
	}

	public MBContainer getContainer() {
		return container;
	}

	public MBScene getScene() {
		return scene;
	}

	public MBTexture getTexture() {
		if (textures == null)
			textures = new MBTexture();
		return textures;
	}

	public RoadNetwork getRoadNetwork() {
		return rn;
	}

	public MBCost getCost() {
		return cost;
	}

	public Singapore getSGInfo() {
		return sgInfo;
	}

	public MBActivity getActivity() {
		return activity;
	}

	public PublicTransit getPublicTransit() {
		return transitNetwork;
	}

	public TrajectoryGraph getTrajGraph() {
		return this.trjGraph;
	}

	public AllToAllDijkstraPublic getAllToAllDijkstra() {
		return allToAllDijkstra;
	}

	public EdgeODPossibility getEdgeODPoss() {
		return edgeODPoss;
	}
}
